﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Collections.Generic;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;
using System.Windows.Threading;
using System.Windows.Media.Imaging;

namespace girapong
{
    public class item: solid
    {
        public Image figure { get; set; }
        private int topClash { get; set; }
        public item(string pathImage, double x0, double y0, screen screenReference, rally rallyReference)
        {
            speed = general.generateRandomOnBottomArrowVectorByModule(4);
            position = new System.Windows.Point(x0, y0);
            figure = new Image();
            dtSpeed = new DispatcherTimer();
            dtSpeed.Interval = TimeSpan.FromMilliseconds(1);

            if (rallyReference != null)
            {
                rallyReference.LayoutRoot.Children.Add(figure);
                this.rallyReference = rallyReference;
                dtSpeed.Tick += new EventHandler(refreshRally);
            }
            figure.Width = 30;
            figure.Height = 30;
            figure.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            figure.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
            figure.Margin = new Thickness(x0, y0, 0, 0);
            figure.Source = new BitmapImage(new Uri(pathImage, UriKind.RelativeOrAbsolute));
            topClash = 0;
            dtSpeed.Start();
        }
        public void changeSpeed(Vector2 speed)
        {
            this.speed = speed;
        }
        private void refreshRally(Object sender, EventArgs e)
        {
            Vector2 vUnitary = new Vector2(speed.X, speed.Y);

            if (speed.X != 0) position.X += vUnitary.X;
            if (speed.Y != 0) position.Y += vUnitary.Y;
            figure.Margin = new Thickness(position.X, position.Y, 0, 0);

            System.Windows.Shapes.Rectangle rU = rallyReference._barPlayer.rectangle;
            if (interseccionRectangulo(rU))
            {
                dtSpeed.Stop();
                rallyReference.LayoutRoot.Children.Remove(figure);
                rallyReference.explodeAll();
                rallyReference.itemList.Remove(this);
            }
            else
            {
                if ((figure.Margin.Top > screen.HEIGHT) || (figure.Margin.Top <= -20))
                {
                    if (topClash > 0)
                    {
                        dtSpeed.Stop();
                        rallyReference.LayoutRoot.Children.Remove(figure);
                        rallyReference.itemList.Remove(this);
                    }
                    else
                    {
                        changeSpeed(new Vector2(speed.X, -speed.Y));
                        topClash++;
                    }
                }
                else if ((figure.Margin.Left >= screen.WIDTH) || (figure.Margin.Left <= 0))
                {
                    speed = new Vector2(-speed.X, speed.Y);
                }

                else if (rallyReference.blockList != null) //choque con algun bloque
                {
                    List<SolidBlock> lista = rallyReference.blockList;
                    if (lista.Count > 0)
                    {
                        for (int i = 0; i < lista.Count; i++)
                        {
                            System.Windows.Shapes.Rectangle r = lista[i].shape;

                            if (base.clashToBlockWhileMovement(figure.Margin.Left, figure.Width, figure.Margin.Top, figure.Height, r) == 0)
                            {
                                changeSpeed(new Vector2(-speed.X, speed.Y));
                                break;
                            }
                            else
                                if (base.clashToBlockWhileMovement(figure.Margin.Left, figure.Width, figure.Margin.Top, figure.Height, r) == 1)
                                {
                                    changeSpeed(new Vector2(speed.X, -speed.Y));
                                    break;
                                }
                        }
                    }
                }
            }
        }
        public Boolean interseccionRectangulo(System.Windows.Shapes.Rectangle rE)
        {
            Thickness margen = figure.Margin;
            return base.rectangleIntersection(margen.Left, figure.Width, margen.Top, figure.Height, rE);
        }
    }
}
